import org.kwis.msp.lcdui.Jlet;
import org.kwis.msp.lcdui.Display;
import org.kwis.msp.db.DataBase;
import org.kwis.msp.db.DataBaseException;
import org.kwis.msp.db.DataBaseRecordException;
import org.kwis.msp.io.FileSystem;

public class UpdateDataBase extends Jlet{

	
	public UpdateDataBase(){  }

	protected void startApp(String[] arg0){
		
		DataBase dataBASE2 = null;
		String dataBaseName="dataBASE2";
		int dbRecordLength=5;

		try	{	
			
			dataBASE2 = DataBase.openDataBase(dataBaseName, dbRecordLength, true);

			String stringSource="WIPIDataBaseEX";

			int iteration = stringSource.length()-4;

			for(int i=0;i<iteration;i++)	{		   
				byte data[] = (stringSource.substring(i,i+5)).getBytes();
				dataBASE2.insertRecord(data,0,5);
			}
			
			databaseState(dataBASE2);

			String updateSource="AZ";
			int updateSourceLength=updateSource.length();
			byte dataUpdate[] = updateSource.getBytes();

			for(int i=0;i<iteration;i++)	{		   
				dataBASE2.updateRecord(i,dataUpdate,0,updateSourceLength);
			}
			
			databaseState(dataBASE2);

			dataBASE2.deleteRecord(3);
			dataBASE2.deleteRecord(6);

			dataBASE2.insertRecord("DELETE".getBytes(),0,5);
			dataBASE2.insertRecord("DELETE".getBytes(),0,5);
			dataBASE2.insertRecord("DELETE".getBytes(),0,5);
			databaseState(dataBASE2);

			dataBASE2.closeDataBase();

		} catch(Exception e){	}	
		
	}

	protected void destroyApp(boolean arg0) {  }

	public void databaseState(DataBase db){

		try{

			System.out.println("===================================================");
			System.out.println("");

			System.out.println("DataBase Name = "+db.getDataBaseName() );

			int mode;
			mode=DataBase.getAccessMode(db.getDataBaseName());
			if(mode==FileSystem.PRIVATE_ACCESS){System.out.println("Access Mode = PRIVATE_ACCESS Mode");}				
			else if(mode==FileSystem.SHARED_ACCESS){System.out.println("Access Mode = SHARED_ACCESS Mode");}			
			else if(mode==FileSystem.SYSTEM_ACCESS){System.out.println("Access Mode = SYSTEM_ACCESS Mode");}

			System.out.println("DataBase Size = "+db.getDataBaseSize() +" Bytes");
			System.out.println("Record Number = "+db.getNumberOfRecords() );
			System.out.println("Record Size   = "+db.getRecordSize()+" Bytes");
			System.out.println("Last Modified Time = "+db.getLastModified());
			System.out.println("Available Size = " + db.getSizeAvailable()+" Bytes");
			System.out.println("");

			System.out.println("Records of "+db.getDataBaseName());
	
			
			
			for(int index=0;index<db.getNumberOfRecords();index++){
				try{
					System.out.println("Record "+ index+" : "+new String(db.selectRecord(index)));
				}catch(DataBaseRecordException dbre){System.out.println("DB Record Exception : " +index);}
			}		 

			System.out.println("");
			System.out.println("===================================================");

		}catch(Exception e){}
				
	}

}
